package xyz.yuzegod.megawalls.listener;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.inventory.ItemStack;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.util.ItemBuilder;

public class SetupListener extends BaseListener
{
    public static Location left;
    public static Location right;
    private ItemStack axe;
    
    public SetupListener(final MegaWalls plugin) {
        super(plugin);
        this.axe = null;
        this.axe = new ItemBuilder(Material.DIAMOND_AXE).setDisplayName("§a§l选择工具").setUnbreakable(true).build();
    }
    
    @EventHandler
    public void onPlayerLogin(final PlayerLoginEvent e) {
        if (!e.getPlayer().isOp()) {
            e.setResult(PlayerLoginEvent.Result.KICK_OTHER);
            e.setKickMessage("§c未发现游戏实例,不能进入该服务器！");
        }
    }
    
    @EventHandler
    public void onPlayerJoin(final PlayerJoinEvent e) {
        final Player p = e.getPlayer();
        p.getInventory().clear();
        p.getInventory().setArmorContents((ItemStack[])null);
        p.getInventory().setItem(0, this.axe);
    }
    
    @EventHandler
    public void onPlayerInteract(final PlayerInteractEvent e) {
        if (e.getPlayer().getItemInHand() == null) {
            return;
        }
        if (e.getPlayer().getItemInHand().equals((Object)this.axe) && (e.getAction() == Action.LEFT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
            if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
                SetupListener.left = e.getClickedBlock().getLocation();
                e.getPlayer().sendMessage("§e选择第一个点:" + SetupListener.left.getBlockX() + " " + SetupListener.left.getBlockY() + " " + SetupListener.left.getBlockZ());
            }
            else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                SetupListener.right = e.getClickedBlock().getLocation();
                e.getPlayer().sendMessage("§e选择第二个点:" + SetupListener.right.getBlockX() + " " + SetupListener.right.getBlockY() + " " + SetupListener.right.getBlockZ());
            }
            e.setCancelled(true);
        }
    }
}
